<template>
    <el-dialog :title="action == 'add' ? '新增' : '编辑'" :model-value="show" @update:model-value="handleValueUpdate"
        @close="handleClose">
        <el-form ref="FormRef" :model="FormData" label-width="100px" :rules="rules">
            <el-form-item label="角色名称" prop="roleName">
                <el-input v-model="FormData.roleName" placeholder="请输入角色名称" />
            </el-form-item>
            <el-form-item label="角色编码" prop="roleCode">
                <el-input v-model="FormData.roleCode" placeholder="请输入角色编码" />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input type="textarea" :rows="2" v-model="FormData.remark" placeholder="请输入备注" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="handleClose">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { ref, defineProps, getCurrentInstance, onMounted } from 'vue'
const { proxy } = getCurrentInstance();
const $Tajax = proxy.$Tajax;
const props = defineProps({
    show: {
        type: Boolean,
        default: false
    },
    action: {
        type: String,
        default: 'add'
    },
    Item: {
        type: Object,
        default: ''
    },

})
const FormRef = ref(null)
const FormData = ref({
    roleName: null,
    roleCode: null,
    remark: null,
})
const emit = defineEmits(['update:show'])

onMounted(() => {
    if (props.action == 'edit') {
        FormData.value = props.Item
    } else {
        FormData.value.parentId = props.Item._id
        console.log(FormData.value.parentId)
    }
    getDeptList()
    getRoleAllList()
})
const deptList = ref([]) // 部门列表
const getDeptList = async () => {
    let res = await $Tajax.getDeptList();
    deptList.value = res
}
const roleList = ref([]); // 角色列表
const getRoleAllList = async () => {
    let list = await proxy.$Tajax.getRoleAllList();
    roleList.value = list;
};
const rules = ref({
    roleName: [
        {
            required: true,
            message: "请输入角色名称",
            trigger: "blur",
        },
    ],
    roleCode: [
        {
            required: true,
            message: "请选择角色code",
            trigger: "blur",
        },
    ],
    remark: [
        { required: false, message: "请输入描述", trigger: "blur" },
    ],
})

const submitForm = () => {
    FormRef.value.validate(async (valid) => {
        if (valid) {
            console.log(FormData.value)
            let params = { ...FormData.value, action: props.action };
            let res = await $Tajax.roleOperate(params);
            if (res) {
                proxy.$message.success("用户创建成功");
                emit('close', true)
                FormRef.value.resetFields()
            }

        }
    })
}

const handleClose = () => {
    emit('update:show', false)
}
const handleValueUpdate = (val) => {
    emit('update:show', val)
}

</script>

<style></style>